﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YJKArchUtils.Utils;

namespace YJKElementFinish.ColumnSurface.Managers
{
	public class ElementCommon
	{
		public static List<Solid> GetElementSolids(UIApplication UIApp, Element element)
		{
			Options options = UIApp.Application.Create.NewGeometryOptions();
			options.DetailLevel = (ViewDetailLevel)3;
			options.IncludeNonVisibleObjects = true;
			GeometryElement geometryElement;
			if (element.GetType() == typeof(FamilyInstance))
			{
				FamilyInstance familyInstance = element as FamilyInstance;
				geometryElement = familyInstance.GetOriginalGeometry(options);
				geometryElement = geometryElement.GetTransformed(familyInstance.GetTransform());
			}
			else
			{
				geometryElement = element.get_Geometry(options);
			}
			List<Solid> list = new List<Solid>();
			if (null == geometryElement)
			{
				return list;
			}
			foreach (GeometryObject geometryObject in geometryElement)
			{
				if (!(geometryObject.GetType() != typeof(Solid)))
				{
					Solid solid = geometryObject as Solid;
					if (solid != null && !Geometry.LessThan(solid.Volume, 0.001))
					{
						list.Add(solid);
					}
				}
			}
			return list;
		}

		public static void GetSolidProjection(Autodesk.Revit.DB.Document doc, List<Solid> solids, ref CurveArray curveAry)
		{
			foreach (Solid solid in solids)
			{
				foreach (object obj in solid.Faces)
				{
					Face face = (Face)obj;
					if (!(face.GetType() != typeof(PlanarFace)))
					{
						PlanarFace planarFace = face as PlanarFace;
						if (Geometry.IsEqual(planarFace.FaceNormal(), XYZ.BasisZ))
						{
							ElementCommon.GetPlanarFaceProjection(doc, planarFace, ref curveAry);
						}
					}
				}
			}
		}

		private static void GetPlanarFaceProjection(Autodesk.Revit.DB.Document doc, PlanarFace pFace, ref CurveArray curveAry)
		{
			foreach (object obj in pFace.EdgeLoops)
			{
				foreach (object obj2 in ((EdgeArray)obj))
				{
					Curve curve = ((Edge)obj2).AsCurve();
					curveAry.Append(curve);
				}
			}
		}

		public static bool CurveSameAS(Curve curve1, Curve curve2)
		{
			if (curve1.GetType() != curve2.GetType())
			{
				return false;
			}
			XYZ endPoint = curve1.GetEndPoint(0);
			XYZ endPoint2 = curve1.GetEndPoint(1);
			XYZ endPoint3 = curve2.GetEndPoint(0);
			XYZ endPoint4 = curve2.GetEndPoint(1);
			if (curve1.GetType() == typeof(Arc))
			{
				Arc arc = curve1 as Arc;
				Arc arc2 = curve2 as Arc;
				if (!Geometry.IsEqual(arc.Center, arc2.Center))
				{
					return false;
				}
				if (!Geometry.IsEqual(endPoint, endPoint3))
				{
					return false;
				}
				if (!Geometry.IsEqual(endPoint2, endPoint4))
				{
					return false;
				}
			}
			else
			{
				if (!(curve1.GetType() == typeof(Line)))
				{
					return false;
				}
				if (!Geometry.IsEqual(endPoint, endPoint3))
				{
					return false;
				}
				if (!Geometry.IsEqual(endPoint2, endPoint4))
				{
					return false;
				}
			}
			return true;
		}
	}
}
